package com.htfyun.factorytest.engineer_test;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.htfyun.factorytest.FTFloatWindowService;
import com.htfyun.factorytest.MainActivity;
import com.htfyun.factorytest.R;
import com.htfyun.factorytest.common.Constants;
import com.htfyun.factorytest.common.FTUtils;
import com.htfyun.factorytest.config.Configuration;
import com.htfyun.factorytest.config.EinkProperty;
import com.htfyun.factorytest.config.NVRomKeys;
import com.htfyun.factorytest.device.DeviceAttr;
import com.htfyun.factorytest.factorytestui.FactoryPCBAActivity;
import com.htfyun.factorytest.factorytestui.FactoryTestItemsEnum;
import com.htfyun.factorytest.utils.executor.AppExecutors;

/**
 * Created by sst on 2018/7/11.
 */

public class FragmentAssistSettings extends BaseFragment implements View.OnClickListener {

    private final static String TAG = FragmentAssistSettings.class.getSimpleName();
    private View btnEnterBootLoader;
    private View btnClearAgingResult;
    private View btnClearPcbaResult;
    private View btnClearAllResult;
    private Configuration config = Configuration.getInstance();

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_engineer_assist_settings;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {
        btnEnterBootLoader = findViewById(R.id.btnEnterBootLoader);
        btnClearAgingResult = findViewById(R.id.btnClearAgingResult);
        btnClearPcbaResult = findViewById(R.id.btnClearPcbaResult);
        btnClearAllResult = findViewById(R.id.btnClearAllResult);

    }

    @Override
    protected void initData() {
        btnEnterBootLoader.setOnClickListener(this);
        btnClearAgingResult.setOnClickListener(this);
        btnClearPcbaResult.setOnClickListener(this);
        btnClearAllResult.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btnEnterBootLoader:
                EinkProperty.enterBootloader();
                break;

            case R.id.btnClearAgingResult:
                FTUtils.clearAgingResult(mContext);
                break;

            case R.id.btnClearPcbaResult:
                clearPcbaResult();
                break;

            case R.id.btnClearAllResult:
                AppExecutors.runOnIoThread(() -> {
                    //[aging]
                    FTUtils.clearAgingResult(mContext);
                    //[function_test] and [function_test_aging]
                    clearFunctionResult();
                });
                //[pcba]
                clearPcbaResult();
                break;

        }
    }

    private void clearFunctionResult() {
        for (FactoryTestItemsEnum itemsEnum : FactoryTestItemsEnum.values()) {
            if (MainActivity.isContinue(itemsEnum)) continue;
            //mmkv.xml
            itemsEnum.setResult(mContext, mContext.getString(R.string.test_result_fail));
            //[function_test] and [function_test_aging] exclude aging
            if (FactoryTestItemsEnum.aging == itemsEnum) {
                Configuration.getInstance().setConfig(Constants.SECTION_AGING, itemsEnum.name(), "0");
                continue;
            }
            for (String item : itemsEnum.getNVRomKey()) {
                //无电磁屏 config.ini只写入触摸屏结果
                if (!DeviceAttr.getRunningDeviceAttr().isExistETP() && NVRomKeys.KEY_FT_ELECTRIC_TP.equals(item)) {
                    continue;
                }
                //无触摸屏 config.ini只写入电磁屏结果
                if (!DeviceAttr.getRunningDeviceAttr().isExistTP() && NVRomKeys.KEY_FT_FINGER_TP.equals(item)) {
                    continue;
                }
                if (itemsEnum.isKeepTestAfterAging()) {
                    itemsEnum.setResultForAfterAging(mContext, mContext.getString(R.string.test_result_fail));
                    Configuration.getInstance().setConfig(Constants.SECTION_FUNCTION_TEST_AGING, itemsEnum.newNVRomKeyForAfterAging(item), "0");
                }
                Configuration.getInstance().setConfig(Constants.SECTION_FUNCTION_TEST, item, "0");
            }
            //function_test = 0 , function_test_aging = 0
            Configuration.getInstance().setConfig(Constants.SECTION_FUNCTION_TEST, Constants.SECTION_FUNCTION_TEST, "0");
            Configuration.getInstance().setConfig(Constants.SECTION_FUNCTION_TEST_AGING, Constants.SECTION_FUNCTION_TEST_AGING, "0");
//            config.removeSection(Constants.SECTION_FUNCTION_TEST);
//            config.removeSection(Constants.SECTION_FUNCTION_TEST_AGING);
        }
    }

    private void clearPcbaResult() {
        AppExecutors.runOnIoThread(() -> {
            FactoryTestItemsEnum.pcba.setResult(mContext, mContext.getString(R.string.test_result_fail));
            EinkProperty.setEinkPCBATestAllResultPassed(false);
            config.setConfig(Constants.SECTION_PCBA, Constants.SECTION_PCBA, "0");
            for (FactoryPCBAActivity.PCBATestItemEnum itemsEnum : FactoryPCBAActivity.PCBATestItemEnum.values()) {
                if (FactoryPCBAActivity.isContinue(DeviceAttr.getRunningDeviceAttr(), itemsEnum)) {
                    continue;
                }
                config.setConfig(Constants.SECTION_PCBA, itemsEnum.name(), "0");
            }
//            config.removeSection(Constants.SECTION_PCBA);
            AppExecutors.postToMainThread(() -> FTFloatWindowService.setPCBABtnShow(true));
        });
    }
}
